<?php

namespace app\models;

use Yii;
use yii\base\Action;
use app\models\AuthItemChild;

class Identity extends Admin implements \yii\web\IdentityInterface
{

    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        $login = UserLogin::findOne(['token'=>$token]);
        if($login){
            $identity = Identity::findOne($login->user_id);
            if($identity->status == 1){
                return $identity;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }

    public function getAuthKey(){

    }


    public function validateAuthKey($authKey){

    }

    /**
     * @param $action Action|string
     * @return bool
     */
    public static function checkAccess($action){
        return true;
        if($action instanceof Action){
            $moduleId     = $action->controller->module->id;
            $controllerId = $action->controller->id;
            $actionId     = $action->id;


            $permissions = [
                "*",
                ".",
                "/$moduleId/*",
                "/$moduleId/$controllerId/*",
                "/$moduleId/$controllerId/$actionId",
            ];
        }else{
            $permissions = [
                "*",
                $action
            ];
        }

        foreach ($permissions as $permission) {
            if (Yii::$app->user->can($permission)) {
                return true;
            }
        }
        return false;
    }

    public static function checkPromission($permission){
        $res = AuthItemChild::getPermission(Yii::$app->user->id,$permission);
        if ($res) {
            return true;
        }
        return false;
    }
}
